function generateLevel()
{
    var levelStyle = document.getElementById("level");
    var level_mask = [];
	
	if ( ( !levelStyle ) || ( levelStyle.value == "static" ) ) // Строим статичный уровень
	{
	     level_mask[0] = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                         [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                         [1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1],
                         [1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1],
                         [1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1],
                         [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1],
                         [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
                         [1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                         [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1],
                         [1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1],
                         [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
                         [1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
                         [1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
                         [1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1],
                         [1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                         [1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1],
                         [1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                         [1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1],
                         [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                         [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];
	}
	else                                                      // Строим динамический уровень
	{
	    var MAP_DIFF = 40; // вероятность постройки стены
        var WALL_MAX_TURN = 80 // макс количество поворотов стены. чем число больше, больше стен и больше кривизна
        var bc = 0;
        
        level_mask[0] = [];
       
        for ( var i = 0; i < BLOCKS_IN_LVL; i++ ) // строю границы
        {
            level_mask[0][i] = [];
            for ( var j = 0; j < BLOCKS_IN_LVL; j++ )
            {
                if ((i == 0) || (j == 0) || (i == BLOCKS_IN_LVL-1) || (j == BLOCKS_IN_LVL-1))
                    level_mask[0][i][j] = 1;
                else
                    level_mask[0][i][j] = 0;
            }
        } 
    
    
        function freeMove(){  // проверка на пустоты
            var zeros = 0;        
            var r;
        
            for ( var i = 0; i < BLOCKS_IN_LVL; i++ )
                for ( var j = 0; j < BLOCKS_IN_LVL; j++ )
                    if (level_mask[0][i][j] == 0) 
                        zeros++;  //количество ноликов
        
            function markCells(y, x){ 
                if ((level_mask[0][y][x] == 0) && (y > 1) && (x > 1) && (y < BLOCKS_IN_LVL-1) && (x < BLOCKS_IN_LVL-1)){
                    level_mask[0][y][x] = 2;
                    markCells(y+1, x);
                    markCells(y-1, x);
                    markCells(y, x+1);
                    markCells(y, x-1);
                    r++; // количество помеченых ноликов
                }
            }        
        
            for ( var i = 1; i < BLOCKS_IN_LVL; i++ )
                for ( var j = 1; j < BLOCKS_IN_LVL; j++ )
                    if (level_mask[0][i][j] == 0) {
                        r = 0;
                        markCells(i, j); 
                        if (r < zeros){ // есть пустоты, сверлим стенку 
                                        // баг: слева мало стен
                            if ((i+1 < BLOCKS_IN_LVL-1) && (level_mask[0][i+1][j] == 1 ))
                                level_mask[0][i+1][j] = 0;
                            else if ((j+1 < BLOCKS_IN_LVL-1) && (level_mask[0][i][j+1] == 1))
                                level_mask[0][i][j+1] = 0;
                            else if  ((i-1 > 1) && (level_mask[0][i-1][j] == 1))
                                level_mask[0][i-1][j] = 0;
                            else if ((j-1 > 1) && (level_mask[0][i][j-1] == 1))
                                level_mask[0][i][j-1] = 0;

                        }
                    }
         
            for ( var i = 0; i < BLOCKS_IN_LVL-1; i++ )  // убираю метки
                for ( var j = 0; j < BLOCKS_IN_LVL-1; j++ )
                    if (level_mask[0][i][j] == 2)
                        level_mask[0][i][j]  = 0;  
        }
    
        function buildWall(x, y, dx, dy){
             if (getRandomInt(1, 100) < MAP_DIFF) {
                 var depth = getRandomInt(1, 3);
                 if (dx == 0) 
                    depth = depth * dy;
                 else if(dy==0)
                    depth = depth * dx;
                 for( var c = 1; c <= depth; c++ ){
                    if((x > 0) && (x < BLOCKS_IN_LVL-1) && (y > 0) && (y < BLOCKS_IN_LVL-1)){   
                     if ((level_mask[0][x+dx][y+dy] != 1) && (level_mask[0][x+dx*2][y+dy*2] != 1)){                    
                        level_mask[0][x][y] = 1;
                        x+=dx;
                        y+=dy;
                        bc++;
                        if (bc < WALL_MAX_TURN){ 
                           buildWall(x, y, -1,  0);
                           buildWall(x, y,  1,  0);
                           buildWall(x, y,  0,  1);
                           buildWall(x, y,  0, -1);
                        }
                     }                 
                    }
                 }             
             }         
        }
    
        for ( var j = 1; j <= BLOCKS_IN_LVL-1; j++ )
        {
            for ( var i = 1; i <= BLOCKS_IN_LVL-1; i++ )
            {
                if (((i==1) || (j==1) || (i==BLOCKS_IN_LVL-2) || (j==BLOCKS_IN_LVL-2)) && (level_mask[0][i][j]==0)) {
                    if (getRandomInt(1, 100) < MAP_DIFF){ 
                        buildWall(i, j,  1,  0);
                        buildWall(i, j, -1,  0);
                        buildWall(i, j,  0,  1);
                        buildWall(i, j,  0, -1);
                        bc=0; 
                    }
                }                
            }
        }     
        freeMove(); 	
        freeMove(); // запускаем второй раз на тот случай если первый раз были удалены не все пустоты
	}
	
    level = [];
    for ( var i = 0; i < BLOCKS_IN_LVL; i++ )
    {
        level[i] = [];
        for ( var j = 0; j < BLOCKS_IN_LVL; j++ )
        {
            level[i][j] = new Block( j * BLOCK_SIDE, i * BLOCK_SIDE, level_mask[0][i][j] );
        }
    }
} 